package com.lihuinch.test.feishu.eventsubscribe.handler.impl;


import com.alibaba.fastjson.JSONObject;
import com.lihuinch.test.feishu.eventsubscribe.constant.TestConstants;
import com.lihuinch.test.feishu.eventsubscribe.handler.FeishuEventHandler;
import com.lihuinch.test.feishu.eventsubscribe.handler.response.FeishuEventSpendVoucherUpdatedV1Response;
import com.lihuinch.test.feishu.eventsubscribe.util.FeishuVouchersApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 飞书凭证事件处理
 *
 * @author：岳彩郑
 * @desc：TODO
 * @date：2022/9/28 15:38
 **/
@Service(FeishuEventHandler.FEI_SHU_EVENT_HANDLER + "_" + TestConstants.FeishuEvent.Contact.SPEND_VOUCHER_UPDATED_V1)
public class FeishuEventSpendVoucherUpdatedV1Handler implements FeishuEventHandler<FeishuEventSpendVoucherUpdatedV1Response> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FeishuVouchersApi feishuVouchersApi;

    @Override
    public FeishuEventSpendVoucherUpdatedV1Response handleEvent(JSONObject param) {

        //todo  1. 这里接收飞书请求
        //事件JSON值转String
        JSONObject event = param.getJSONObject("event");
        String form_code = event.getString("form_code");
        String bpm_status = event.getString("bpm_status");
        String page_size = "2";//固定值
        String value = "form_code=" + form_code + "&" + "page_size=" + page_size;
        logger.info("获取到了单据号：" + form_code);

        //如果状态等于completed 则调用接口
        if ("completed".equalsIgnoreCase(bpm_status)) {

            logger.info("开始异步请求飞书凭证接口，获取数据");
            //todo 3. 异步处理任务
            //调用凭证接口
            feishuVouchersApi.FeishuVouchersApi(value);
        }


        logger.info("FeishuEventSpendVoucherUpdatedV1Response");

        //todo  2. 返回成功200
        return new FeishuEventSpendVoucherUpdatedV1Response();
    }
}
